package boardgamer.yavalath.client.mainmenu;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import boardgamer.common.client.Button;
import boardgamer.common.client.ClickableButton;
import boardgamer.common.client.layout.BitmapLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.MutableTextLayout;
import boardgamer.yavalath.client.RenderHelper;
import boardgamer.yavalath.client.SharedObjects;
import boardgamer.yavalath.client.resources.ImageHandler;
import boardgamer.yavalath.client.resources.SoundHandler;

import javax.annotation.Nullable;

/**
 * @author garysoed
 */
public class MainMenuButton extends ClickableButton {

  private static final Paint BACKGROUND_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);

  static {
    BACKGROUND_PAINT.setColorFilter(new LightingColorFilter(Color.rgb(255, 0, 0), 0));
  }

  public MainMenuButton(String text, EventHandler clickHandler, boolean proVersion) {
    super(
        new InnerButton(text, clickHandler, proVersion),
        new BitmapLayout(ImageHandler.cutBigButtonGlow, BACKGROUND_PAINT));
  }

  private static class InnerButton implements Button {

    private static final float TEXT_MAX_WIDTH_RATIO = 0.9f;
    private static final float TEXT_MAX_HEIGHT_RATIO = 0.9f;
    private static final int TEXT_MAX_CHAR_COUNT = 11;

    private final BitmapLayout buttonBackground;
    @Nullable private final BitmapLayout lock;
    private final MutableTextLayout textLayout;
    private final LayoutGroup group;
    private final EventHandler clickHandler;

    public InnerButton(String text, EventHandler clickHandler, boolean proVersion) {
      this.clickHandler = clickHandler;

      boolean isProEnabled = SharedObjects.instance().getProFeatures().isEnabled();
      boolean isFeatureDisabled = false && !isProEnabled && proVersion;

      if (isFeatureDisabled) {
        this.buttonBackground
            = new BitmapLayout(ImageHandler.cutBigButtonDisabled, BACKGROUND_PAINT);
        this.lock = new BitmapLayout(ImageHandler.lock, BACKGROUND_PAINT);
      } else {
        this.buttonBackground = new BitmapLayout(ImageHandler.cutBigButton, BACKGROUND_PAINT);
        this.lock = null;
      }

      Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      textPaint.setColor(Color.BLACK);
      textPaint.setTypeface(ImageHandler.headingFont);
      textPaint.setTextSize(RenderHelper.instance().getMaxFittingHeightTextSize(
          buttonBackground.getBoundingRect().height() * TEXT_MAX_HEIGHT_RATIO,
          ImageHandler.headingFont));
      this.textLayout = new MutableTextLayout(
          textPaint,
          buttonBackground.getBoundingRect().width() * TEXT_MAX_WIDTH_RATIO,
          TEXT_MAX_CHAR_COUNT,
          LayoutGroup.HorizontalAlign.RIGHT,
          ImageHandler.boundSizeRatio.get(ImageHandler.headingFont));
      this.textLayout.setText(text);

      this.group = new LayoutGroup()
          .add(buttonBackground).atCurrentRect()
          .add(textLayout).atCurrentRect()
          .verticalAlign(LayoutGroup.VerticalAlign.CENTER)
          .horizontalAlign(LayoutGroup.HorizontalAlign.CENTER);
      if (isFeatureDisabled) {
        this.group.add(lock).relativeToEdge(buttonBackground, LayoutGroup.Direction.RIGHT, 0);
      }
    }

    @Override
    public void draw(Canvas canvas) {
      buttonBackground.draw(canvas);
      textLayout.draw(canvas);
      if (lock != null) {
        lock.draw(canvas);
      }
    }

    @Override
    public void handleEvent(float eventX, float eventY, int eventAction) {
      if (eventAction == MotionEvent.ACTION_UP && getCollisionBox().contains(eventX, eventY)) {
        SoundHandler.playWave(SoundHandler.buttonSound);
        clickHandler.onEvent(eventX, eventY, eventAction);
      }
    }

    @Override
    public RectF getCollisionBox() {
      return buttonBackground.getBoundingRect();
    }

    @Override
    public void setBoundingRect(RectF boundingRect) {
      group.setBoundingRect(boundingRect);
    }

    @Override
    public RectF getBoundingRect() {
      return group.getBoundingRect();
    }
  }
}
